Skip to content

Unify listen-only mode into can.Bus(listen_only=...) - #2149

Open
zariiii9003 wants to merge 8 commits into
hardbyte:mainfrom
zariiii9003:listen-only-split-v3
Open

zariiii9003 wants to merge 8 commits into
hardbyte:mainfrom
zariiii9003:listen-only-split-v3

Conversation

@zariiii9003

@zariiii9003 zariiii9003 commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary of Changes

Makes listen-only mode part of the official bus API and moves it out of BusState, as proposed in #736: a listen_only flag replaces the use of BusState.PASSIVE as a mode switch, so BusState no longer mixes the CAN error state with a configuration option.

 can.Bus(interface, channel, ..., listen_only=None)  # None: from config, else False
   load_config()                                      # "listen_only" normalized to bool
+  cls(channel, listen_only=<bool>, **kwargs)         # always passed to the bus class

 class BusABC:
+  __new__(cls, ..., listen_only=True)                # CanInterfaceNotImplementedError
+                                                     # unless cls.__init__ declares listen_only
+  __init__(..., *, listen_only: bool = False)
+  listen_only            -> bool                     # property, False by default
+  listen_only = <bool>   -> NotImplementedError      # unless switchable at runtime
   send(msg)              -> CanOperationError        # if in listen-only mode

Listen-only mode is the bus monitoring mode of ISO 11898-1 (§10.14): the node receives frames but never drives the bus dominant, so it sends no ACKs, error or overload flags (unlike restricted operation mode, §10.15). The name listen_only follows iproute2 and the existing vendor options.

Contract, documented on BusABC: an interface supports listen-only mode only if its constructor declares a listen_only parameter. For every other interface, built-in or third-party, BusABC.__new__ raises CanInterfaceNotImplementedError for listen_only=True before the constructor runs, so no driver or device is opened; this works for can.Bus and for direct instantiation. False is always accepted. In listen-only mode nothing is put on the wire and send() raises CanOperationError.

Interface listen_only=True Runtime setter Deprecated alternative
cantact monitor mode – monitor
kvaser silent driver mode – driver_mode
nixnet, slcan, vector listen-only mode – – (already had listen_only)
pcan, systec listen-only mode bus.listen_only = ... state= argument, bus.state = BusState.PASSIVE
seeedstudio silent mode – operation_mode="silent" / "loopback_and_silent"
virtual send disabled – –
all others, and third-party interfaces without a declared listen_only CanInterfaceNotImplementedError – –
  • can.cli: new --listen-only / --<prefix>-listen-only switch (iproute2 keyword); can.logger --passive / --active are deprecated and hidden from --help, --passive now opens the bus with listen_only=True.
  • Deprecations start in 4.7.0 and end in 5.0.0. Until then the deprecated paths keep their behaviour apart from the warning, including switching pcan/systec back to BusState.ACTIVE at runtime.
  • BusState itself is unchanged; the error state part of Meaning of can.BusABC.state #736 (ErrorState, error counters) is not addressed here.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe): deprecation of interface specific listen-only options

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Evidence

tox -e py,lint,type,docs passes on every commit (final state: 741 passed, 66 skipped). Mocked listen-only tests per interface check that the hardware mode is set and that send() does not reach the driver:

test_listen_only (virtual, vector, kvaser, pcan, cantact, slcan, systec)
  bus = can.Bus(..., listen_only=True)
  assert driver was put into listen-only / silent mode
  assert bus.listen_only
  with raises(CanOperationError): bus.send(msg)
  assert driver transmit function not called
  • Before: can.Bus(interface="socketcan", channel="vcan0", listen_only=True) silently ignores listen_only and the bus transmits.
    After: raises CanInterfaceNotImplementedError before the socket is opened.
  • Before: a plugin whose constructor only takes **kwargs silently ignores listen_only=True and transmits.
    After: raises CanInterfaceNotImplementedError before the plugin constructor runs (test_bus.py::test_bus_listen_only_rejected_if_not_declared, through can.Bus and directly).
  • Before: VectorBus(..., listen_only=True).send(msg) is accepted and only looped back locally.
    After: raises CanOperationError, like every other listen-only bus.

The real-hardware Vector test (test_vector.py::test_listen_only) is skipped without the XL driver and still needs a run on a machine with Vector hardware.

Merge Danger

Door: two-way

Nothing is removed; deprecated options keep working until 5.0.0, and every change can be reverted before a release.

Blast Radius: plugins

can.Bus now always passes listen_only to the bus class, so a third-party interface whose __init__ accepts neither listen_only nor **kwargs fails with TypeError; all 11 plugins listed in doc/plugin-interface.rst accept **kwargs, so none of them breaks. For 9 of them (canine, remote, sontheim, zlgcan, cando, RP1210, damiao, hub, coe) listen_only=True now raises CanInterfaceNotImplementedError instead of being silently ignored while the bus transmits. candle declares listen_only and keeps working, but its bus.listen_only reports False until it updates the new property. send() now raises in listen-only mode for users of the existing Vector listen_only, seeedstudio silent modes, cantact monitor, kvaser driver_mode and pcan/systec BusState.PASSIVE. CantactBus (monitor) and PcanBus (state) lose a positional parameter, which shifts later positional arguments.

🤖 Generated with AI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant